$(function() {
	$('#addForm').submit(function() {
		$.ajax({
			url: '/wms/salmg/addSalmg.xhtml',
			data: $('#addForm').serialize(),
			type: "POST",
			dataType : "text",
			error : function() {
				alert('请求失败!');
			},
			success: function(result) {
				if(result=="success") {
					alert("增加成功!");
					closeDialogAdd();
					addSalmgReset();
					reloadTable();
				} else {
					alert(result);
				}
			}
		});
		return false;
	});
	ajaxTable();
	setDialogSearch();
	closeDialogSearch();
	setDialogAdd();
	closeDialogAdd();
	if(globalMaps.get('2') == undefined) {
		$("#toolbarAdd").hide();
	}
});
function ajaxTable() {
	$('#salmgTable').datagrid({
		toolbar : [ {
			id : 'toolbarAdd',
			text : '增加',
			iconCls : 'icon-add',
			handler : function() {
				//openDialogPurmb('','');
				openDialogAdd();
			}
		}, '-', {
			id : 'toolbarSearch',
			text : '查找',
			iconCls : 'icon-search',
			handler : function() {
				openDialogSearch();
			}
		}
		],
		frozenColumns:[[
			{field:"ck", checkbox:true}
		]],
		pageNumber : 1,
		loadMsg : '数据加载中,请稍后...',
		pageList : [ 10, 30, 50, 100 ],
		onLoadError : function() {
			alert('数据加载失败!');
		},
		queryParams : {
			mgaa : $("#searchMgaa").combobox("getValue"),
			mgab : $("#searchMgab").val(),
			mgac : $("#searchMgac").combobox("getValue"),
			startMgad : $("#searchStartMgad").datebox("getValue"),
			endMgad : $("#searchEndMgad").datebox("getValue")
		},
		onClickRow : function(rowIndex, rowData) {
			$('#salmgTable').datagrid('unselectRow', rowIndex);
		},
		onDblClickRow : function(rowIndex, rowData) {
			//openDialogPurmb(rowData.maaa,rowData.maab);
			openDialogBody(rowData.mgaa,rowData.mgab);
	    }
	}).datagrid('acceptChanges');
}
function reloadTable() {
	$('#salmgTable').datagrid('reload');
	$('#salmgTable').datagrid('clearSelections');
}
function setDialogSearch() {
	$('#salmgSearch').dialog({
		title : '查询采购单',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function openDialogSearch() {
	$('#salmgSearch').dialog('open');
}
function closeDialogSearch() {
	$('#salmgSearch').dialog('close');
}
function searchSalmg() {
	var queryParams = $('#salmgTable').datagrid('options').queryParams;
	queryParams.mgaa = $("#searchMgaa").combobox("getValue");
	queryParams.mgab = $("#searchMgab").val();
	queryParams.mgac = $("#searchMgac").combobox("getValue");
	queryParams.startMgad = $("#searchStartMgad").datebox("getValue");
	queryParams.endMgad = $("#searchEndMgad").datebox("getValue");
	reloadTable();
	closeDialogSearch();
}
function mgacFormater(value, row, index) {
	if(row.mgac == "Y") {
		return "已审核";
	} else if(row.mgac == "C") {
		return "已结束";
	} else if(row.mgac == "V") {
		return "已作废";
	}
	return "未审核";

}

//----------------新增Begin
function setDialogAdd() {
	$('#salmgAdd').dialog({
		title : '新增',
		modal : true,
		left : 50,
		top : 50,
		collapsible : true,
		resizable : true
	});
}
function openDialogAdd() {
	addSalmgReset();
	$('#salmgAdd').dialog('open');
	
}
function closeDialogAdd() {
	$('#salmgAdd').dialog('close');
}
function addSalmg() {
	var validateResult = $("#addForm").form('validate');
	if(validateResult == false) {
		return false;
	}
	$('#addForm').submit();
}
function addSalmgReset() {
	$("#addmgaa").combobox("select", "");
	$("#addmgab").val("");
}
//----------------新增End
//----------------采购订单单身
function openDialogBody(mgaa,mgab) {
	var url = '/wms/salmh/salmhMain.xhtml?mgaa=' + mgaa +'&mgab='+mgab;		
	window.showModalDialog(url,"","dialogWidth=1000px; dialogHeight=700px; help=no; status=no; scroll=yes; resizable=no;");
	$('#salmgTable').datagrid('clearSelections');
	reloadTable();
}
